//
//  MNDDepositChangePhoneViewController.m
//  minidai
//
//  Created by 容贤郁 on 2018/3/21.
//  Copyright © 2018年 迷你金服. All rights reserved.
//

#import "MNDDepositChangePhoneViewController.h"
#import "NSString+ReplaceAsterisk.h"
#import "UIButton+JKCountDown.h"
#import "UIImage+JKColor.h"

@interface MNDDepositChangePhoneViewController ()

@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *captchaTextField;
@property (weak, nonatomic) IBOutlet UIButton *submitButton;

@end

@implementation MNDDepositChangePhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.phoneLabel.text = [NSString stringWithFormat:@"您当前绑定的手机号是%@，修改后的手机号主要用于接受江西银行存管账户动态通知。", [self.phoneNum replaceWithAsteriskAtRange:NSMakeRange(3, 4)]];
    
    __block UITextField *phoneTextField = self.phoneTextField;
    [self.phoneTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 11) {
            phoneTextField.text = [x substringToIndex:11];
        }
    }];
    
    __block UITextField *captchaTextField = self.captchaTextField;
    [self.captchaTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        if (x.length > 6) {
            captchaTextField.text = [x substringToIndex:6];
        }
    }];
    
    RAC(self.submitButton, enabled) = [RACSignal combineLatest:@[self.phoneTextField.rac_textSignal, self.captchaTextField.rac_textSignal] reduce:^id (NSString *phone, NSString *captcha){
        return @(phone.length >= 11 && captcha.length >= 4);
    }];
    
    [self.submitButton setBackgroundImage:[UIImage imageNamed:@"invest_result_continue_btn"] forState:UIControlStateNormal];
    [self.submitButton setBackgroundImage:[UIImage jk_imageWithColor:kColorLightGray] forState:UIControlStateDisabled];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)sendCpatchaAction:(UIButton *)sender {
    
    if (self.phoneTextField.text.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入新手机号"];
        
        return;
    }
    
    [NetworkProgressHUD showHud];
    
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"user_phone": self.phoneTextField.text, @"channel": @"app", @"srvtxcode": @"mobileModifyPlus"}];
    [APIManager sendRequestWithPath:API_DEPOSIT_SEND_CAPTCHA parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"success"]];
            [sender jk_startTime:30 title:@"重新发送" waitTittle:@"秒后重发"];
            
        } else {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

- (IBAction)submitAction:(UIButton *)sender {
    if (self.phoneTextField.text.length <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入新手机号"];
        return;
    }
    
    if (self.captchaTextField.text <= 0) {
        [NetworkProgressHUD showHudMsg:@"请输入手机验证码"];
        return;
    }
    
    [NetworkProgressHUD showHud];
    WeakSelf(self);
    
    NSDictionary *params = [MNDSign signedParamWithDict:@{@"user_phone": self.phoneTextField.text, @"phone_captcha": self.captchaTextField.text, @"channel": @"app"}];
    
    [APIManager sendRequestWithPath:API_DEPOSIT_PHONE_UPDATE parameters:params method:kXMHTTPMethodPOST onSuccess:^(ResponseModel *responseModel) {
        
        if (responseModel.status == ResponseSuccess) {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"success"]];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakself.navigationController popViewControllerAnimated:YES];
            });
            
        } else {
            [NetworkProgressHUD showHudMsg:responseModel.msg[@"error"]];
        }
    } onFailure:^(NSError *error) {
        [NetworkProgressHUD showHudNetworkError:error];
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 2;
}

@end
